<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>在线考试系统-学生管理</title>
    <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<link href="/res/css/style.css" rel="stylesheet">
    <style type="text/css"></style>
</head>
<body>

<?php include(APPLICATION_PATH.'application/views/index/header.html');?>

<div id="container">
    <div class="container-left">
        <?php include(APPLICATION_PATH.'application/views/index/left.html');?>
    </div>
    <div class="container-right well">
		<div class="tabSet"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addstudetModal">添加学生</button></div>
		<table class="table table-bordered">
		   <thead>
			  <tr>
				<th>学号</th>
				<th>姓名</th>
				<th>性别</th>
				<th>操作</th>
			  </tr>
		   </thead>
		   <tbody>
                <?php foreach($list as $v) {?>
                  <tr>
                    <td><?php echo $v['number']?></td>
                    <td><?php echo $v['name'];?></td>
                    <td><?php
                        if($v['sex'] == 1){
                            echo '男';
                        }else{
                            echo '女';
                        }
                    ?></td>
                    <td>
                        <button type="button" class="btn btn-primary update_class" data-toggle="modal" data-target="#updateclassModal" _id="<?php echo $v['_id'];?>" student_name="<?php echo $v['name'];?>" >编辑</button>
                        <button type="button" class="btn btn-danger delete_class" data-toggle="modal" data-target="#deleteclassModal" _id="<?php echo $v['_id'];?>">删除</button>
                    </td>
                  </tr>
                <?php }?>
		   </tbody>
		</table>
    </div>
</div>
<div class="modal fade" id="addstudetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title" id="myModalLabel">添加学生</h4>
        </div>
        <div class="modal-body">
           <h5>学号</h5>
		   <input class="form-control" name="number" type="text" placeholder="" style="width:200px;">
		   <h5>姓名</h5>
		   <input class="form-control" name="name" type="text" placeholder="" style="width:200px;">
		   <h5>性别</h5>
		    男<input type="radio" name="sex" value="1">
            女<input type="radio" name="sex" value="0">
            <h5>密码</h5>
            <input class="form-control" name="password" type="password" placeholder="" style="width:200px;">
            <h5>确认密码</h5>
            <input class="form-control" name="confirm_pwd" type="password" placeholder="" style="width:200px;">
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" id="addStudent"> 添加</button>
        </div>
      </div><!-- /.modal-content -->
   </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="updateclassModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">修改班级</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="update_data_id" value=""/>
                <h5>学号</h5>
                <input class="form-control" name="update_number" type="text" placeholder="" style="width:200px;">
                <h5>姓名</h5>
                <input class="form-control" name="update_name" type="text" placeholder="" style="width:200px;">
                <h5>性别</h5>
                男<input type="radio" name="update_sex" value="1">
                女<input type="radio" name="update_sex" value="0">
                <h5>密码</h5>
                <input class="form-control" name="update_password" type="password" placeholder="" style="width:200px;">
                <h5>确认密码</h5>
                <input class="form-control" name="update_confirm_pwd" type="password" placeholder="" style="width:200px;">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="saveClass" class="btn btn-primary"> 保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="deleteclassModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="mySmallModalLabel">删除学生</h4>
            </div>
            <input type="hidden" id="delete_data_id" value=""/>
            <div class="modal-body">
                <button type="button" style="margin-left:35px" class="btn btn-default delete-modal-cancel" data-dismiss="modal">&nbsp;取&nbsp;&nbsp;消&nbsp;&nbsp;</button>
                <button type="button" style="margin-left:30px" class="btn btn-danger delete-modal-ensure">&nbsp;&nbsp;删&nbsp;&nbsp;除&nbsp;&nbsp;</button>
            </div>
        </div><!-- /.modal-content -->
    </div>
</div>
<script src="/res/js/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>

<script src="/res/js/toast.js"></script>

<script>
$(function () { 
	$('#myModal').modal({
		keyboard: true
	})

    $('#addStudent').click(function(){
        var number = $('[name="number"]').val();
        var name = $('[name="name"]').val();
        var sex = $('input[name="sex"]:checked').val();
        var password = $('[name="password"]').val();
        var confirm_pwd = $('[name="confirm_pwd"]').val();

       if(!check_student(number, name, sex, password, confirm_pwd)){
           return false;
       }

        var url = '/student/add';
        var data = {"number":number, "name":name, "sex":sex, "password":password};
        $.post(url, data, function(ret){
            if(ret.en == 200) {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
                setTimeout(function(){
                    window.location.href = '/student/index';
                } , 2000);
            } else {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
            }
        });
    });

    /** "删除" 触发模态框显示 */
    $('.delete_class').click(function(){
        var _id = $(this).attr('_id');
        $('#delete_data_id').val(_id);
    });

    /** 执行 "删除" */
    $('.delete-modal-ensure').click(function(){
        var _id = $('#delete_data_id').val();
        var request_api = '/student/delete';
        if(_id != undefined && _id != '') {
            $.post(request_api ,{"_id":_id} ,function(ret) {
                if(ret.en == 200) {
                    new Toast({context:$('body'),top:200 ,time:1000 ,message:ret.em}).success();
                    setTimeout(function(){
                        $('#deleteclassModal').modal('hide');
                        window.location.reload();
                    } , 1000);
                } else {
                    new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
                }
            });
        } else {
            new Toast({context:$('body'),top:200 ,time:1000, message:"参数不全"}).error();
        }
    });

    /** "修改" 触发模态框显示 */
    $('.update_class').click(function(){
        var _id = $(this).attr('_id');
        $('#update_data_id').val(_id);
        if($(this).parent().prev().text() == "男"){
            $('input[name="update_sex"][value=1]').attr('checked',true);
        }else{
            $('input[name="update_sex"][value=0]').attr('checked',true);
        }
        $('[name="update_name"]').val($(this).parent().prev().prev().text());
        $('[name="update_number"]').val($(this).parent().prev().prev().prev().text());
    });

    /**
     * 编辑学生，并保存
     */
    $('#saveClass').click(function(){
        var update_data_id = $('#update_data_id').val();
        var update_number = $('[name="update_number"]').val();
        var update_name = $('[name="update_name"]').val();
        var update_sex = $('input[name="update_sex"]:checked').val();
        var update_password = $('[name="update_password"]').val();
        var update_confirm_pwd = $('[name="update_confirm_pwd"]').val();

       if(!check_student(update_number, update_name, update_sex, update_password, update_confirm_pwd)){
           return false;
       }

        var url = '/student/save';
        var data = {"_id":update_data_id, "number":update_number, "name":update_name, "sex":update_sex, "password":update_password};
        $.post(url, data, function(ret){
            if(ret.en == 200) {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
                setTimeout(function(){
                    window.location.href = '/student/index';
                } , 2000);
            } else {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
            }
        });
    });

    function check_student(number, name, sex, password, confirm_pwd){
        if(number == ''){
            new Toast({context:$('body'),top:200 ,time:2000, message:'请输入学号'}).error();
            return false;
        }
        if(name == ''){
            new Toast({context:$('body'),top:200 ,time:2000, message:'请输入姓名'}).error();
            return false;
        }

        if(!(sex == 0 || sex == 1)){
            new Toast({context:$('body'),top:200 ,time:2000, message:'请选择性别'}).error();
            return false;
        }
        if(password == ''){
            new Toast({context:$('body'),top:200 ,time:2000, message:'请输入密码'}).error();
            return false;
        }
        if(confirm_pwd == ''){
            new Toast({context:$('body'),top:200 ,time:2000, message:'请确认密码'}).error();
            return false;
        }
        if(password != confirm_pwd ){
            new Toast({context:$('body'),top:200 ,time:2000, message:'两次密码输入不一致,请重新输入'}).error();
            $('[name="password"]').val('');
            $('[name="confirm_pwd"]').val('');
            return false;
        }

        return true;
    }
});
</script>

</body>
</html>